package cn.good.solr.service.impl;

import cn.good.solr.pojo.DoctorSolr;
import cn.good.solr.service.DoctorSolrService;
import cn.good.solr.vo.DoctorSolrVO;
import cn.good.solr.vo.FindSolrVO;
import cn.good.utils.Constant;
import cn.good.utils.EmptyUtils;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DoctorSolrServiceImpl implements DoctorSolrService {

    @Resource
    private SolrClient solrClient;


    /**
     * 分页查询医生信息列表
     * @param vo
     * @return
     * @throws Exception
     */
    @Override
    public DoctorSolrVO searchDoctorListPage(FindSolrVO vo) throws Exception {
        SolrQuery query=new SolrQuery("*:*");
        Integer currPage=(EmptyUtils.isEmpty(vo.getPageNo())?0:vo.getPageNo()-1);
        Integer pageSize=(EmptyUtils.isEmpty(vo.getPageSize())?Constant.PAGE_NO:vo.getPageSize());
        Integer start=currPage*pageSize;
        query.setStart(start);
       //设置每页显示数量
        query.setRows(pageSize);
        if(EmptyUtils.isNotEmpty(vo.getDesk1())){
            query.addFilterQuery("dName1:"+vo.getDesk1()+"");
            if(EmptyUtils.isNotEmpty(vo.getDesk2())){
                query.addFilterQuery("dName2:"+vo.getDesk2()+"");
            }
        }
        QueryResponse queryResponse=solrClient.query(query);
        List<DoctorSolr> list=queryResponse.getBeans(DoctorSolr.class);
        DoctorSolrVO doctorSolrVO=new DoctorSolrVO();
        doctorSolrVO.setDoctorSolrList(list);
        doctorSolrVO.setPageNo(currPage);
        return doctorSolrVO;
    }


}
